package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.core.dao.database.ICustomerCollectDao;
import com.singfan.core.dao.database.model.CustomerCollect;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class CustomerCollectDaoImpl extends DaoBaseGeneric<CustomerCollect> implements ICustomerCollectDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public boolean isCollect(long customerId, long collectId, int collectType) {
        String sql = "SELECT * FROM " + getTableName(customerId) + " WHERE customer_id=? AND collect_id=? AND collect_type=?";

        return isExist(customerId, sql, customerId, collectId, collectType);
    }

    @Override
    public void collect(long customerId, long collectId, int collectType) {
        CustomerCollect customerCollect = new CustomerCollect();
        customerCollect.setCustomerId(customerId);
        customerCollect.setCollectId(collectId);
        customerCollect.setCollectType(collectType);
        customerCollect.setCreateTime(new Date());

        insertWithAutoIncrement(customerId, customerCollect);
    }

    @Override
    public void collectCancel(long customerId, long collectId, int collectType) {
        String sql = "DELETE FROM " + getTableName(customerId) + " WHERE customer_id=? AND collect_id=? AND collect_type=?";
        execute(customerId, sql, customerId, collectId, collectType);
    }

    @Override
    public List<CustomerCollect> selectCollectList(long customerId, int collectType, int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(customerId) + " WHERE customer_id=? AND collect_type=? limit ?,?";
        return selectList(customerId, sql, customerId, collectType, pageIndex * pageSize, pageSize);
    }
}
